home *** CD-ROM | disk | FTP | other *** search
- /* Program PRIMER.C
-
- Prime number generator. Generates MAXNPRIM prime numbers
- and prints them in groups of 500.
-
- Program by Harry M. Murphy, 1 September 1988.
-
- Based on the Pascal prime number generator, PRIMER.PAS
- by Harry M. Murphy. */
-
- #include <stdio.h>
- #include <dos.h>
- #include <math.h>
-
- /* Global defined constants. */
- #define LISTSIZE 500
- #define MAXNPRIM 3500
- #define OUTFILE "PRIMER.LIS"
- #define TBLSIZE 1000
-
- /* Function prototypes. */
- void getprime();
- void page();
- void prntlist();
- double secnds(double);
- void space(int);
-
- /* Global variables. */
- long inc;
- long list[LISTSIZE+1]; /* The prime number printing list. */
- int nprime;
- FILE *out;
- int pge;
- long prime;
- long table[TBLSIZE+1]; /* The stored prime number table. */
- double trun;
- long try;
- double tzero;
-
- /* ------------------------------ */
-
- void page()
-
- { fprintf(out,"\f\n");
- return; }
-
- /* ------------------------------ */
-
- double secnds(double tzero)
-
- /* This function returns the number of seconds since tzero
- as a DOUBLE number.
-
- Requires #include <dos.h>.
-
- Function by Harry M. Murphy, 9 September 1988. */
-
- {
- union REGS in,out;
- double secs;
-
- in.h.ah = 0x2c;
- intdos(&in,&out);
- secs = (((((out.h.ch)*60.0 +
- out.h.cl)*60.0 +
- out.h.dh)*100.0 +
- out.h.dl)/100.0) - tzero;
- if (secs < 0.0)
- return secs + 86400.0;
- else
- return secs; }
-
- /* ------------------------------ */
-
- void space(int n)
-
- { int i;
-
- i = 1;
- while (i <= n) {fprintf(out," "), i++; }
- return; }
-
- /* ------------------------------ */
-
- void getprime()
-
- { int i;
- long maxdiv;
- int remain;
- double tryflt;
-
- do
- { inc = 6-inc;
- try += inc;
- tryflt = try;
- maxdiv = floor(sqrt(tryflt + 0.5));
- i = 2;
- do
- { i++;
- remain = try%table[i]; }
- while (remain && (table[i] < maxdiv)); }
- while (!remain);
- prime = try;
- return; }
-
- /* ------------------------------ */
-
- void prntlist()
-
- { int i,j,k;
-
- page();
- pge++;
- printf("Writing page %3d.\r",pge);
- space(71);
- fprintf(out,"Page%5d\n\n",pge);
- fprintf(out,"%8dth -%5dth Primes.",nprime-499,nprime);
- space(36);
- fprintf(out,"%7ld to%7ld\n\n",list[1],list[500]);
- for (i=1; i <= 50; i++)
- { j = i;
- k = i+450;
- do
- { fprintf(out,"%8ld",list[j]);
- j = j+50; }
- while (j <= k);
- fprintf(out,"\n"); }
- return; }
-
- /* ------------------------------ */
-
- void main()
-
- { int inlist, intbl;
-
- tzero = secnds(0.0);
- if ((out = fopen(OUTFILE,"w")) == NULL)
- {printf("Can't open %s!",OUTFILE);
- exit(1); }
- printf("Generating %d primes and writing them to %s.\n",
- MAXNPRIM,OUTFILE);
- pge = 0;
- table[0] = 1;
- table[1] = 2;
- table[2] = 3;
- table[3] = 5;
- try = 5;
- inc = 4;
- for (intbl = 4; intbl <= TBLSIZE; intbl++)
- { getprime();
- table[intbl] = prime; }
- inlist = 0;
- nprime = 0;
- for (intbl = 1; intbl <= TBLSIZE; intbl++)
- { list[++inlist] = table[intbl];
- nprime++;
- if (inlist == LISTSIZE)
- { prntlist();
- inlist = 0; } }
- do
- { getprime();
- list[++inlist] = prime;
- nprime++;
- if (inlist == LISTSIZE)
- { prntlist();
- inlist = 0; } }
- while ((nprime < MAXNPRIM) | (inlist != 0));
- page();
- fprintf(out," End of prime number tables.\n");
- trun = secnds(tzero);
- fprintf(out," Run time =%5.1f seconds.\n",trun);
- printf("Run time =%5.1f seconds.\n",trun);
- fclose(out);
- printf("The tables are generated.\n");
- printf("The results are in file %s.",OUTFILE); }
-
- /* End of Program PRIMER. */